package ce.com.newbluedemo.k2;

import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEBluetoothProxyBase;
import ce.com.cenewbluesdk.scan.CEScanDevBase;
import ce.com.cenewbluesdk.scan.CEScanDev_4;
import ce.com.newbluedemo.R;

public class ActivityK2SearchBlueTooth extends K2BaseActivity implements View.OnClickListener {
    ListView listView;
    MyAdapter myAdapter;
    ArrayList<BluetoothDevice> arrayList = new ArrayList<>();
    CEScanDevBase ceScanDevBase;
    private CEBluetoothProxyBase proxy;
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1000) {
                arrayList.add((BluetoothDevice) msg.obj);
            }
            myAdapter.setList(arrayList);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        findViewById(R.id.end_scan).setOnClickListener(this);
        findViewById(R.id.start_scan).setOnClickListener(this);

        ceScanDevBase = new CEScanDev_4(this, new CEScanDevBase.FindBlueTooth() {
            @Override
            public void findDev(BluetoothDevice device, int rssi, byte[] scanRecord) {
                Log.e("liu", "address=" + device.getAddress());
                boolean have = false;
                for (BluetoothDevice b : arrayList
                        ) {
                    if (device.getAddress().equals(b.getAddress())) {
                        have = true;
                        break;
                    }
                }
                if (!have) {
                    Message message = new Message();
                    message.what = 1000;
                    message.obj = device;
                    uiHandler.sendMessage(message);
                }

            }
        });

        proxy = BleFactory.getInstance().getK2Proxy();


        listView = (ListView) findViewById(R.id.list_view);
        myAdapter = new MyAdapter(arrayList);
        listView.setAdapter(myAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ceScanDevBase.stopScan();
                proxy.connectDev(arrayList.get(position).getAddress());
            }
        });
        ceScanDevBase.startScan();

    }


    @Override
    public void receiveData(Message message) {
        if (message.what == CEBC.BLUE_CONNECT_STATE_CHANGE) {
            if (message.arg1 == CEBC.BLUE_CONNECTED) {
                Toast.makeText(this, "蓝牙连接成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this, TestK2BlueActivity.class));
                finish();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_scan:
                ceScanDevBase.startScan();
                arrayList.clear();
                myAdapter.setList(arrayList);

                break;
            case R.id.end_scan:
                ceScanDevBase.stopScan();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {
        public ArrayList<BluetoothDevice> list;
        public MyAdapter(ArrayList<BluetoothDevice> list) {
            this.list = list;
        }
        public void setList(ArrayList<BluetoothDevice> list) {
            this.list = list;
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(ActivityK2SearchBlueTooth.this).inflate(R.layout.item_bluetooth_address, parent, false);
            BluetoothDevice bluetoothDevice = list.get(position);
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView address = (TextView) view.findViewById(R.id.address);
            address.setText(bluetoothDevice.getAddress());
            name.setText(bluetoothDevice.getName());
            return view;
        }
    }


}
